package com.anjubao.parking.municipal.common.config;

import java.util.Set;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

//import com.ctrip.framework.apollo.Config;
//import com.ctrip.framework.apollo.model.ConfigChangeEvent;
//import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
//import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;

/**
 * 监听logging level配置刷新事件
 * 
 * @author frank
 */
@Service
public class LoggerLevelRefresher implements ApplicationContextAware {
	private ApplicationContext applicationContext;

	private static final Logger logger = LoggerFactory.getLogger(LoggerLevelRefresher.class);

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

	}

//	@ApolloConfig
//	private Config config;

//	@PostConstruct
//	private void initialize() {
//		refreshLoggingLevels(config.getPropertyNames());
//	}
//
//	@ApolloConfigChangeListener
//	private void onChange(ConfigChangeEvent changeEvent) {
//		refreshLoggingLevels(changeEvent.changedKeys());
//	}

//	private void refreshLoggingLevels(Set<String> changedKeys) {
//		boolean loggingLevelChanged = false;
//		for (String changedKey : changedKeys) {
//			if (changedKey.startsWith("logging.level.")) {
//				loggingLevelChanged = true;
//				break;
//			}
//		}
//
//		if (loggingLevelChanged) {
//			logger.info("Refreshing logging levels");
//
//			/**
//			 * refresh logging levels
//			 *
//			 * @see org.springframework.cloud.logging.LoggingRebinder#onApplicationEvent
//			 */
//			applicationContext.publishEvent(new EnvironmentChangeEvent(changedKeys));
//
//			logger.info("Logging levels refreshed");
//		}
//	}
//
//	@Override
//	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//		this.applicationContext = applicationContext;
//	}

}